package com.zzh.lib.recorder.utils;


import com.zzh.lib.htts.HTTSLib;

import java.io.File;

/**
 * Created by ZZH on 2022/12/25.
 *
 * @Date: 2022/12/25
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: lame操作工具类
 */
public class LameUtils {

    static {
        System.loadLibrary("mp3lame");
    }

    /**
     * Initialize LAME.
     *
     * @param inSamplerate  input sample rate in Hz.
     * @param inChannel     number of channels in input stream.
     * @param outSamplerate output sample rate in Hz.
     * @param outBitrate    brate compression ratio in KHz.
     * @param quality       <p>quality=0..9. 0=best (very slow). 9=worst.</p>
     *                      <p>recommended:</p>
     *                      <p>2 near-best quality, not too slow</p>
     *                      <p>5 good quality, fast</p>
     *                      7 ok quality, really fast
     */
    public native static void init(int inSamplerate, int inChannel,
                                   int outSamplerate, int outBitrate, int quality);

    /**
     * Encode buffer to mp3.
     *
     * @param bufferLeft  PCM data for left channel.
     * @param bufferRight PCM data for right channel.
     * @param samples     number of samples per channel.
     * @param mp3buf      result encoded MP3 stream. You must specified
     *                    "7200 + (1.25 * buffer_l.length)" length array.
     * @return <p>number of bytes output in mp3buf. Can be 0.</p>
     * <p>-1: mp3buf was too small</p>
     * <p>-2: malloc() problem</p>
     * <p>-3: lame_init_params() not called</p>
     * -4: psycho acoustic problems
     */
    public native static int encode(short[] bufferLeft, short[] bufferRight,
                                    int samples, byte[] mp3buf);

    /**
     * Flush LAME buffer.
     * <p>
     * REQUIRED:
     * lame_encode_flush will flush the intenal PCM buffers, padding with
     * 0's to make sure the final frame is complete, and then flush
     * the internal MP3 buffers, and thus may return a
     * final few mp3 frames.  'mp3buf' should be at least 7200 bytes long
     * to hold all possible emitted data.
     * <p>
     * will also write id3v1 tags (if any) into the bitstream
     * <p>
     * return code = number of bytes output to mp3buf. Can be 0
     *
     * @param mp3buf result encoded MP3 stream. You must specified at least 7200
     *               bytes.
     * @return number of bytes output to mp3buf. Can be 0.
     */
    public native static int flush(byte[] mp3buf);

    /**
     * Close LAME.
     */
    public native static void close();

    public static File createDefaultFileUnderDir(File dir, String ext) {
        if (dir == null)
            return null;

        if (ext == null)
            ext = "";

        try {
            long current = System.currentTimeMillis();
            File file = new File(dir, current + ext);
            while (file.exists()) {
                current++;
                file = new File(dir, current + ext);
            }
            file.createNewFile();
            return file;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 创建视频录制存储文件夹
     *
     * @return 文件夹绝对路
     */
    public static String createVideoDir() {
        String parent = HTTSLib.getContext().getExternalCacheDir().getAbsolutePath() + "/zzh/record/video";
        File file1 = new File(parent);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        return file1.getAbsolutePath();
    }

    public static String getStorageMp4(String s) {
        File file;

        String parent = HTTSLib.getContext().getExternalCacheDir().getAbsolutePath() + "/zzh/record/video";
        File file1 = new File(parent);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        file = new File(parent, s + ".mp4");

        return file.getPath();
    }

    public static String getStorageMp4(String parent, String s) {
        File file;
        File file1 = new File(parent);
        if (!file1.exists()) {
            file1.mkdir();
        }
        file = new File(parent, s + ".mp4");

        return file.getPath();
    }
}
